using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Common;



namespace IGlobalist.Cms.Admin.Control
{
    public partial class ClassAddEdit : AdminBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void DoBindData(object sender, EventArgs e)
        {
            ClassTree classTree = (ClassTree)sender;           
            BindClass(Convert.ToInt64(classTree.SelectedValue));
        }

        public void BindClass(long ID)
        {
            ICmsClass cmsClass = new CmsClass();
            cmsClass = ClassController.GetClass(ID);            
            ddlClassType.SelectedValue = Convert.ToString(cmsClass.ClassTypeID);
            tbDescription.Text = cmsClass.Description;
            tbName.Text = cmsClass.Name;
            tbName.Enabled=false;
            ddlClassType.Enabled = false;
            ddlUsageType.SelectedValue = Convert.ToInt16( cmsClass.UsageType).ToString();
            classPropertyAddEdit.BindProperty(cmsClass.PropertyList);
            ViewState["Class"] = cmsClass;
            lblStatus.Text = "Edit Class";
            lblError.Text = "";
        }

        public void DoNewClass(object sender, EventArgs e)
        {
            ddlClassType.DataBind();
            ViewState["Class"] = null;
            InitNew();

        }

        public void DoSaveClass(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() == "")
            {
                lblError.Text = "Must enter name";
                return;
            }
            else
            {
                lblError.Text = "";
                ICmsClass cmsClass = new CmsClass();
                bool IsFinish = false;
                if (ViewState["Class"] != null)
                {
                    cmsClass = (ICmsClass)ViewState["Class"];
                }

                if (Convert.ToInt16(ddlClassType.SelectedValue) == (short)CmsObjectType.ContentCategory)
                {
                    PropertyList plist = classPropertyAddEdit.PropertyList;
                    BaseProperty basePro = new BaseProperty();
                    basePro.Name = "Type";
                    basePro.Description = "Type";
                    basePro.DisplayName = "Type";
                    basePro.InputType = InputType.TextBox;
                    basePro.Type = DataType.Text;
                    basePro.PropertyType = PropertyType.Main;
                    basePro.Value = ddlTypePr.SelectedValue.ToString();
                    plist.Add(basePro);
                    cmsClass.PropertyList = plist;
                }
                else
                    cmsClass.PropertyList = classPropertyAddEdit.PropertyList;

                cmsClass.ClassTypeID = Convert.ToInt16(ddlClassType.SelectedValue);
                cmsClass.Description = tbDescription.Text;
                cmsClass.DisplayName = tbName.Text;
                cmsClass.Name = tbName.Text.Replace(" ", "");
                cmsClass.UsageType = (UsageType)Convert.ToInt16(ddlUsageType.SelectedValue);


                if (ViewState["Class"] != null)
                {
                    ClassController.UpdateClass(cmsClass);
                    IsFinish = true;
                    lblStatus.Text = "Update Successful";
                }
                else
                {
                    if (ClassController.GetClassByName(cmsClass.Name) == null)
                    {
                        ClassController.InsertClass(cmsClass);
                        lblError.Text = "";
                        IsFinish = true;
                        //lblStatus.Text = "Save Successful";
                        InitNew();
                    }
                    else
                        lblError.Text = "Name is exist";
                }
                if (!IsFinish) lblStatus.Text = "Add new or update class fail";
            }

        }

        public void DoCopyClass(object sender, EventArgs e)
        {
            ViewState["Class"] = null;
            tbName.Text = "";
            tbName.Enabled = true;
        }

        public void InitNew()
        {
            tbName.Enabled = true;
            ddlClassType.Enabled = true;
            tbName.Text = "";
            tbName.Focus();
            tbDescription.Text="";
            PropertyList propertyList = new PropertyList();
            propertyList = MainProperty.GetPropertyList(Convert.ToInt16(ddlClassType.Items[0].Value));
            classPropertyAddEdit.BindProperty(propertyList);
            if (Convert.ToInt16(ddlClassType.SelectedValue) == (short)CmsObjectType.ContentCategory)
            {
                ddlTypePr.Visible = true;
                lblType.Visible = true;
            }
            lblStatus.Text = "Add New Class";
            lblError.Text = "";
        }

        protected void ddlClassType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt16(ddlClassType.SelectedValue) == (short)CmsObjectType.ContentCategory)
            {
                ddlTypePr.Visible = true;
                lblType.Visible = true;
            }
            PropertyList propertyList = new PropertyList();
            propertyList= MainProperty.GetPropertyList(Convert.ToInt16(ddlClassType.SelectedValue));
            classPropertyAddEdit.BindProperty(propertyList);

           
        }
    }
}
